package controllers

import (
	"github.com/gofiber/fiber/v2"
	"gorm.io/gorm"

	"main/src/models"
	"main/src/utils"
)

func DelUserHandler(c *fiber.Ctx) error {
	db := c.Locals("db").(*gorm.DB)
	user := models.User{Username: c.Params("username", "")}
	if user.Username == "" {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request"})
	}

	if err := utils.GetUser(db, &user, &models.User{}); err != nil {
		return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "User not found"})
	}

	err := utils.DeleteUser(db, &user)
	if err != nil {
		return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to delete user"})
	}

	return c.JSON(fiber.Map{"message": "User deleted successfully", "user": user.Username})
}
